package com.fitness.dao;

import com.fitness.entity.Reservation;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 预约表数据访问对象，继承 BaseDAO，实现预约相关数据库操作
 */
public class ReservationDAO extends BaseDAO {

    /**
     * 插入预约信息
     * @param reservation 预约实体对象
     * @return 插入是否成功（true/false）
     */
    public boolean insertReservation(Reservation reservation) {
        String sql = "INSERT INTO 预约表(会员ID, 项目ID, 预约时间, 预约状态) VALUES (?, ?, ?, ?)";
        int affectedRows = executeUpdate(sql,
                reservation.getMemberId(),
                reservation.getProjectId(),
                reservation.getReservationTime(),
                reservation.getStatus());
        return affectedRows > 0;
    }

    public List<Reservation> getAllReservations() {
        List<Reservation> reservationList = new ArrayList<>();
        String sql = "SELECT * FROM 预约表";
        ResultSet rs = executeQuery(sql);
        try {
            while (rs != null && rs.next()) {
                Reservation reservation = new Reservation();
                reservation.setReservationId(rs.getInt("预约ID"));
                reservation.setMemberId(rs.getInt("会员ID"));
                reservation.setProjectId(rs.getInt("项目ID"));
                reservation.setReservationTime(rs.getTimestamp("预约时间"));
                reservation.setStatus(rs.getString("预约状态"));
                reservationList.add(reservation);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeResources(null, null, rs);
        }
        return reservationList;
    }
}